<?php
/**
* Author	: TRUONG KIM PHUNG
* Class		: USER
* Description
	* Manage user information by index
	* Get
	* Add
	* Update
	* Delete
* Contact	:
	- Email : truongkimphung1982@gmail.com
	- Y!	: truongkimphung1982
	- Skype	: truongkimphung1982
**/
class Ecore_User extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct()
	{
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}
	/**
	 * CONVERT ARRAY TO OBJECT
	 * @PARAM
			- $arrResult
	 * @RESULT
			- Object
	 */
	public function toObjUser($arrResult) {
		$objUser = null;		
		if(isset($arrResult[0])) {
			$objUser->id 				= isset($arrResult[0]['userid']) 			? $arrResult[0]['userid'] 			: 0;			
			$objUser->hex 				= isset($arrResult[0]['hexid']) 			? $arrResult[0]['hexid'] 			: 0;
			$objUser->username			= isset($arrResult[0]['username']) 			? $arrResult[0]['username'] 		: '';
			$objUser->password 			= isset($arrResult[0]['password']) 			? $arrResult[0]['password'] 		: '';			
			$objUser->email 			= isset($arrResult[0]['email']) 			? $arrResult[0]['email'] 			: '';			
			$objUser->fullname 			= isset($arrResult[0]['fullname']) 			? $arrResult[0]['fullname'] 		: '';			
			$objUser->gender 			= isset($arrResult[0]['gender']) 			? $arrResult[0]['gender'] 			: 0;			
			$objUser->education			= isset($arrResult[0]['education']) 		? $arrResult[0]['education'] 		: 0;			
			$objUser->education_name	= isset($arrResult[0]['education_name']) 	? $arrResult[0]['education_name'] 	: '';			
			$objUser->birthdate			= isset($arrResult[0]['birthdate']) 		? $arrResult[0]['birthdate'] 		: '';			
			$objUser->mobile			= isset($arrResult[0]['mobile']) 			? $arrResult[0]['mobile'] 			: '';			
			$objUser->addr				= isset($arrResult[0]['addr']) 				? $arrResult[0]['addr'] 			: '';			
			$objUser->district			= isset($arrResult[0]['district']) 			? $arrResult[0]['district'] 		: 0;	
			$objUser->district_name		= isset($arrResult[0]['district_name']) 	? $arrResult[0]['district_name'] 	: '';			
			$objUser->province			= isset($arrResult[0]['province']) 			? $arrResult[0]['province'] 		: 0;			
			$objUser->province_name		= isset($arrResult[0]['province_name']) 	? $arrResult[0]['province_name'] 	: '';
			$objUser->state				= isset($arrResult[0]['state']) 			? $arrResult[0]['state'] 			: 0;			
			$objUser->country			= isset($arrResult[0]['country']) 			? $arrResult[0]['country'] 			: COUNTRY_DEFAULT;	
			$objUser->country_name		= isset($arrResult[0]['country_name']) 		? $arrResult[0]['country_name'] 	: '';			
			$objUser->introduce			= isset($arrResult[0]['introduce']) 		? $arrResult[0]['introduce'] 		: '';						
			$objUser->occupation		= isset($arrResult[0]['occupation'])	 	? $arrResult[0]['occupation'] 		: 0;			
			$objUser->occupation_name	= isset($arrResult[0]['occupation_name']) 	? $arrResult[0]['occupation_name'] 	: '';	
			$objUser->graduation		= isset($arrResult[0]['graduation']) 		? $arrResult[0]['graduation'] 		: '';	
			$objUser->type				= isset($arrResult[0]['type']) 				? $arrResult[0]['type'] 			: 0;			
			$objUser->status			= isset($arrResult[0]['status']) 			? $arrResult[0]['status'] 			: 0;			
			$objUser->srcid				= isset($arrResult[0]['srcid']) 			? $arrResult[0]['srcid'] 			: 0;			
			$objUser->lastlogin			= isset($arrResult[0]['lastlogin']) 		? $arrResult[0]['lastlogin'] 		: 0;			
			$objUser->postdate			= isset($arrResult[0]['postdate']) 			? $arrResult[0]['postdate'] 		: 0;	
			$objUser->imageid			= isset($arrResult[0]['imageid']) 			? $arrResult[0]['imageid'] 			: 0;	
			$objUser->image_path		= isset($arrResult[0]['image_path']) 		? $arrResult[0]['image_path'] 		: '';	
		}
		return $objUser;
	}	
	/**
	 * GET INFORMATION USER BY ID
	 * @PARAM
			- $intUserId
	 * @RESULT
			- Object | NULL
	 */
	public function get($intUserId) {
		try {			
			$this->prepare('call users_get(?);');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$arrData = $this->fetchAll();					
			return isset($arrData) ? $this->toObjUser($arrData) : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::get - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * GET INFORMATION USER BY HEX
	 * @PARAM
			- $intHexId
			- $intType	: {0:return object;1:return userid}
	 * @RESULT
			- Object | NULL | INTERGER
	 */
	public function getUserByHex($intHexId, $intType=0) {
		try {
			$this->prepare('call users_get_hex(?);');
			$this->bindParam(1, $intHexId, PDO::PARAM_INT, 11);
			$arrData 	= $this->fetchAll();
			$intUserId	= isset($arrData[0]['userid']) ? $arrData[0]['userid'] : 0;
			return $intType ? $intUserId : $this->get($intUserId);
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::getUserByHex - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * GET INFORMATION USER BY USERNAME
	 * @PARAM
			- $strUsername
	 * @RESULT
			- Object | NULL
	 */
	public function getUserByUsername($strUsername, $intType=0) {
		try {
			$this->prepare('call users_get_username(?);');
			$this->bindParam(1, $strUsername, PDO::PARAM_STR, 30);
			$arrData 	= $this->fetchAll();
			$intUserId	= isset($arrData[0]['userid']) ? $arrData[0]['userid'] : 0;            
			return $intType ? $intUserId : $this->get($intUserId);
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::getUserByUsername - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * GET INFORMATION USER BY EMAIL
	 * @PARAM
			- $strEmail
			- $strPassword	: Assign result
			- $intStatus	: Assign result
			- $intType		: {0:return object;1:return userid}
	 * @RESULT
			- Object | NULL
	 */
	public function getUserByEmail($strEmail,&$strPassword='',&$intStatus=0, $intType=0) {
		try {
			$this->prepare('call users_get_email(?);');
			$this->bindParam(1, $strEmail, PDO::PARAM_STR, 150);
			$arrData 		= $this->fetchAll();
			$strPassword	= isset($arrData[0]['password']) ? $arrData[0]['password'] : '';			
			$intStatus		= isset($arrData[0]['status']) ? $arrData[0]['status'] : -1;
			$intUserId		= isset($arrData[0]['userid']) ? $arrData[0]['userid'] : 0;
			return $intType ? $intUserId : $this->get($intUserId);
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::getUserByEmail - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * REGISTER USER
	 * @PARAM
		- $intUserId	: Assign result
		- $strUsername
		- $strPassword
		- $strEmail
		- $intGender	: {1:male,2:female}
		- $intEducation
		- $strBirthdate	
		- $intType		: {1:direct,2:facebook,3:google,4:yahoo}
		- $intSrcId		: User's invited
	 * @RESULT: errCode {
					0	: Success
					1	: Username is exists
					2	: Email is exists
				}	 
	 **/
	public function add( &$intUserId,
						 $strUsername,
						 $strPassword,
						 $strEmail,
						 $strFullname,
						 $intGender,
						 $intEducation,
						 $strBirthdate,
						 $strMobile,
						 $intType=1,
						 $intSrcId=0,
						 $intNewsLetter=0,
						 $strAddr='',
						 $intProvince=0,
						 $strCountry=COUNTRY_DEFAULT,
						 $strGraduation='',
						 $strIntroduce=''
					) {
		try {
			$this->prepare('call users_add(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);');
			$this->bindParam(1, $strUsername	, PDO::PARAM_STR, 30);
			$this->bindParam(2, $strPassword	, PDO::PARAM_STR, 64);
			$this->bindParam(3, $strEmail		, PDO::PARAM_STR, 150);
			$this->bindParam(4, $strFullname	, PDO::PARAM_STR, 150);
			$this->bindParam(5, $intGender		, PDO::PARAM_INT, 1);
			$this->bindParam(6, $intEducation	, PDO::PARAM_INT, 11);	
			$this->bindParam(7, $strBirthdate	, PDO::PARAM_STR, 10);
			$this->bindParam(8, $strMobile		, PDO::PARAM_STR, 20);
			$this->bindParam(9, $intType		, PDO::PARAM_INT, 1);
			$this->bindParam(10, $intSrcId		, PDO::PARAM_INT, 11);			
			$this->bindParam(11, $intNewsLetter	, PDO::PARAM_INT, 4);			
			$this->bindParam(12, $strAddr		, PDO::PARAM_STR, 150);			
			$this->bindParam(13, $intProvince	, PDO::PARAM_INT, 11);			
			$this->bindParam(14, $strCountry	, PDO::PARAM_STR, 2);			
			$this->bindParam(15, $strGraduation	, PDO::PARAM_STR, 150);			
			$this->bindParam(16, $strIntroduce	, PDO::PARAM_STR, 500);			
			$arrData 		= $this->fetchAll();
			$intUserId		= isset($arrData[0]['userid']) ? $arrData[0]['userid'] : 0;
			$intError		= isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::add - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * UPDATE INFORMATION USER
	 * @PARAM
		- $intAdminId
		- $intUserId	
		- $strFullname
		- $intGender
		- $intEducation
		- $strBirthdate
		- $strMobile
		- $strAddr
		- $intDistrict
		- $intProvince
		- $strState
		- $strCountry		
	 * @RESULT: errCode {
					0	: Success
					1	: You're not permission
				}	 
	 **/
	public function update( $intAdminId,
							 $intUserId,						 
							 $strFullname,
							 $intGender,
							 $intEducation,
							 $strBirthdate,
							 $strMobile,
							 $strAddr,
							 $intDistrict,
							 $intProvince,
							 $strState,
							 $strCountry
					) {
		try {
			$this->prepare('call users_update(?,?,?,?,?,?,?,?,?,?,?,?);');
			$this->bindParam(1 , $intAdminId	, PDO::PARAM_INT, 11);
			$this->bindParam(2 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(3 , $strFullname	, PDO::PARAM_STR, 150);
			$this->bindParam(4 , $intGender		, PDO::PARAM_INT, 1);
			$this->bindParam(5 , $intEducation	, PDO::PARAM_INT, 11);	
			$this->bindParam(6 , $strBirthdate	, PDO::PARAM_STR, 10);
			$this->bindParam(7 , $strMobile		, PDO::PARAM_STR, 20);
			$this->bindParam(8 , $strAddr		, PDO::PARAM_STR, 20);
			$this->bindParam(9 , $intDistrict	, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intProvince	, PDO::PARAM_INT, 11);
			$this->bindParam(11, $strState		, PDO::PARAM_STR, 2);
			$this->bindParam(12, $strCountry	, PDO::PARAM_STR, 2);			
			$arrData 		= $this->fetchAll();
			$intError		= isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::update - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * CHANGE EMAIL USER
	 * @PARAM		
		- $intUserId		
	 * @RESULT: errCode {
					0	: Success
					1	: Email is exists
					2	: Current email
				}	 
	 **/
	public function updateEmail( $intUserId ) {
		try {
			$this->prepare('call users_update_email(?);');
			$this->bindParam(1 , $intUserId	, PDO::PARAM_INT, 11);			
			$arrData 	= $this->fetchAll();
			$intError	= isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updateEmail - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * UPDATE LASTLOGIN
	 * @PARAM		
		- $intUserId			  
	 **/
	public function updateLastLogin( $intUserId ) {
		try {
			$this->prepare('call users_update_lastlogin(?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			if($this->execute()) {
				// clear cache
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updateLastLogin - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * CHANGE PASSWORD
	 * @PARAM		
		- $intAdminId
		- $intUserId	
		- $strPassword	
     * @RESULT errCode {
					0	: Success
					1	: You're not permission
				}
		
	 **/
	public function updatePassword( $intAdminId, $intUserId, $strPassword ) {
		try {
			$this->prepare('call users_update_password(?,?,?);');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $strPassword	, PDO::PARAM_STR, 64);			
			$this->bindParam(3, $intAdminId		, PDO::PARAM_INT, 11);			
			$arrData 	= $this->fetchAll();
			$intError	= isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updatePassword - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * CHANGE STATUS
	 * @PARAM		
		- $intAdminId
		- $intUserId	
		- $intStatus {-2: user cancelate; 1: active; 2:suspend; 3:admin delete; 4:report}
		- $intPerm
	 * @RESULT errCode {
					0	: Success
					1	: You're not permission
					2	: Account's not change
				}
	 **/
	public function updateStatus( $intAdminId, $intUserId, $intStatus, $intPerm=0 ) {
		try {			
			$this->prepare('call users_update_status(?,?,?,?);');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intStatus		, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $intAdminId		, PDO::PARAM_INT, 11);			
			$this->bindParam(4, $intPerm		, PDO::PARAM_INT, 4);			
			$arrData 	= $this->fetchAll();
			$intError	= isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * UPDATE INTRODUCE
	 * @PARAM		
		- $intUserId
		- $strContent
	 **/
	public function updateIntroduce( $intUserId, $strContent ) {
		try {
			$this->prepare('call users_update_introduce(?,?);');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $strContent	, PDO::PARAM_STR, 255);			
			if($this->execute()) {
				//clear cache
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updateIntroduce - ' .$e->getMessage());			
			return null;
		}
	}
/***************************************************/
/** USER STAT */
/***************************************************/
	/**
	 * GET USER STAT
	 * @PARAM
			- $intUserId
	 * @RESULT
			- Object | NULL
	 */
	public function getUstat($intUserId) {
		try {
			$this->prepare('call ustats_get(?);');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$arrData = $this->fetchAll();
			return isset($arrData[0]) ? $arrData[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::getUstat - ' .$e->getMessage());			
			return null;
		}
	}
/***************************************************/
/** USER TOKEN */
/***************************************************/
	/**
	 * GENERATE TOKEN FOR USER
	 * @PARAM				
		- $intUserId	
		- $intType {1:act;2:reset password;3:change email}
		- $strToken
		- $intExp
	 * @RESULT errCode {
					1	: Success
					0	: Error
				}
	 **/
	public function addUpdateUtoken( $intUserId, $intType, $strToken, $intExp ) {
		try {
			$this->prepare('call utokens_addupdate(?,?,?,?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intType	, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $strToken	, PDO::PARAM_STR, 150);			
			$this->bindParam(4, $intExp		, PDO::PARAM_INT, 6);			
			if($this->execute()) {
				return 1;
			}
			return 0;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::addUpdateUtoken - ' .$e->getMessage());			
			return null;
		}
	}
	/**
	 * DELETE TOKEN FOR USER
	 * @PARAM				
		- $intUserId	
		- $intType
		- $strToken
	 * @RESULT errCode {
					0	: Success
					1	: Error
				}
	 **/
	public function deleteUtoken( $intUserId, $intType, $strToken) {
		try {
			$this->prepare('call utokens_delete(?,?,?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intType	, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $strToken	, PDO::PARAM_STR, 150);				
			$arrData = $this->fetchAll();
			return isset($arrData[0]['errCode']) ? $arrData[0]['errCode'] : -1;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::deleteUtoken - ' .$e->getMessage());			
			return null;
		}
	}
/***************************************************/
/** USER PENDING FIELD */
/***************************************************/
	/**
	 * ADD RECORD USER PENDING
	 * @PARAM				
		- $intUserId	
		- $intType {1:email;2:mobile}
		- $strValue
	 * @RESULT errCode {
					1	: Success
					0	: Error
				}
	 **/
	public function addUpdateUpending( $intUserId, $intType, $strValue) {
		try {
			$this->prepare('call upendings_addupdate(?,?,?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intType	, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $strValue	, PDO::PARAM_STR, 150);			
			if($this->execute()) {
				return 1;
			}
			return 0;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::addUpdateUpending - ' .$e->getMessage());			
			return null;
		}
	}
/***/
	public function getUimage( $intOffset, $intLimit, &$intTotal=0) {
		try {
			$this->prepare('call uimages_getall(?,?);');
			$this->bindParam(1, $intOffset	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intLimit	, PDO::PARAM_INT, 4);			
			$arrResult = $this->fetchAll();
			$intTotal  = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::getUimage - ' .$e->getMessage());			
			return null;
		}
	}
	public function addUimage($intUserId, $strName, $strPath, $intAvatar, $intAlbumId=0) {
		try {
			$this->prepare('call uimages_add(?,?,?,?,?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $strName	, PDO::PARAM_STR, 100);			
			$this->bindParam(3, $strPath	, PDO::PARAM_STR, 150);			
			$this->bindParam(4, $intAvatar	, PDO::PARAM_INT, 4);			
			$this->bindParam(5, $intAlbumId	, PDO::PARAM_INT, 11);			
			$this->execute();
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::addUimage - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateUimage($intImageId, $intUserId, $strName, $strPath, $intAlbumId=0) {
		try {
			$this->prepare('call uimages_update(?,?,?,?,?);');
			$this->bindParam(1, $intImageId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intUserId	, PDO::PARAM_INT, 11);		
			$this->bindParam(3, $strName	, PDO::PARAM_STR, 100);			
			$this->bindParam(4, $strPath	, PDO::PARAM_STR, 150);			
			$this->bindParam(5, $intAlbumId	, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			$strImage	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : null;
			if(isset($strImage) && $strImage != '') {
				$this->deleteUimageF($strImage);
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_User::updateUimage - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteUimageF($strImg) {		
		if(file_exists(DIR_AVATAR_FULL . $strImg))
			@unlink(DIR_AVATAR_FULL . $strImg);
		if(file_exists(DIR_AVATAR_THUMB . $strImg))
			@unlink(DIR_AVATAR_THUMB . $strImg);;
	}
}
?>